package mirrormonkey.util.netevent.queue;

import junit.framework.Assert;
import mirrormonkey.util.netevent.queue.SimpleEventQueue;
import mirrormonkey.util.netevent.queue.tools.MockEvent;

import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

public class SimpleEventQueueTest {

	private static final int EVENT_COUNT = 100;

	private static SimpleEventQueue q;

	private static MockEvent[] events;

	@BeforeClass
	public static void setUpClass() {
		q = new SimpleEventQueue();
		events = new MockEvent[EVENT_COUNT];
		for (int i = 0; i < EVENT_COUNT; i++) {
			events[i] = new MockEvent();
		}
	}

	@After
	public void tearDown() {
		MockEvent.reset(events);
	}

	/**
	 * Tests if events are added to the queue correctly and if their invocation
	 * is delayed
	 */
	@Test
	public void testAdd() {
		for (MockEvent e : events) {
			q.add(e);
		}
		Assert.assertEquals(EVENT_COUNT, q.getSize());
		for (MockEvent e : events) {
			Assert.assertFalse(e.wasCalled());
		}
	}

	/**
	 * Tests if events are invoked and if they are invoked in order of receipt
	 */
	@Test
	public void testInvokeOrder() {
		q.processEvents();
		int i = 0;
		for (MockEvent e : events) {
			Assert.assertTrue(e.wasCalled());
			Assert.assertEquals(i++, e.callOrder);
		}
	}

	/**
	 * Tests if events are consumed after they have been invoked
	 */
	@Test
	public void testEventConsume() {
		Assert.assertEquals(0, q.getSize());
		q.processEvents();
		for (MockEvent e : events) {
			Assert.assertFalse(e.wasCalled());
		}
	}

}
